package com.luofan.controller;

import com.luofan.common.result.Result;
import com.luofan.common.util.MinioUtil;
import com.luofan.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

/**
 * 文件控制器
 */
@RestController
@RequestMapping("/file")
@CrossOrigin
public class FileController {

    @Autowired
    private FileService fileService;
    @Autowired
    private MinioUtil minioUtil;

    //上传文件
    @PostMapping
    public Result<String> upload(MultipartFile file) {
        String filename = fileService.upload(file);
        return Result.ok(filename);
    }

    //删除文件
    @DeleteMapping("/{fileName}")
    public Result<Boolean> deleteFile(@PathVariable String fileName) {
        Boolean b = minioUtil.removeFile(fileName);
        return Result.ok(b);
    }

    //下载文件
    @GetMapping("/download/{fileName}")
    public ResponseEntity<StreamingResponseBody> download(@PathVariable String fileName) {
        System.out.println("download...");
        return minioUtil.download(fileName);
    }

}
